package com.erp.module.yuliaofreight.YuliaoFreight

import androidx.appcompat.app.AlertDialog
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.EditText
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.bean.YuliaoCangweiBean
import com.erp.module.yuliaofreight.YuliaoFreightConteract
import com.erp.myAdapter.YuliaoCangweiAdapter
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_yuliao_cangwei.*
import kotlinx.android.synthetic.main.dialog_etyuliaocangwei.*
import kotlinx.android.synthetic.main.header_knoya.*

class YuliaoCangweiActivity : BaseActivity(), AdapterView.OnItemClickListener, View.OnClickListener, YuliaoFreightConteract.YuliaoFreightView {
    var updialog: AlertDialog?=null
    lateinit var cangweilist: MutableList<YuliaoCangweiBean>
    lateinit var P: IYuliaoCangweiP
    lateinit var adapter: YuliaoCangweiAdapter
    override fun getLayoutId(): Int {
        return R.layout.activity_yuliao_cangwei
    }

    override fun initCreate() {
        cangweilist = ArrayList<YuliaoCangweiBean>()
        P = IYuliaoCangweiP(this,this,cangweilist)
        adapter = YuliaoCangweiAdapter(cangweilist, this)
        LvCangwei.adapter = adapter
        LvCangwei.onItemClickListener = this
        BtBianma.setOnClickListener(this)
        P.getCangweiData()
        tv_header_title.text = "仓位管理"
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        updialog = Myutil.showupdatedialog(this, "获取数据", false)
    }

    override fun onClick(v: View?) {
        when(v){
            BtBianma -> {
                val dialog = AlertDialog.Builder(this).create()
                dialog.setView(EditText(this))
                dialog.show()
                dialog.setContentView(R.layout.dialog_etyuliaocangwei)
                dialog.CangweiEt.setLines(20)
                val window = dialog.window
                dialog.title.text = "新建仓位"
                val p = window?.attributes // 获取对话框当前的参数值
                p?.width = (sp.getInt("width", 0) * 0.9).toInt() // 宽度设置为屏幕的0.65
                window?.attributes = p
                dialog.LeftBt.setOnClickListener {
                    var s=dialog.CangweiEt.text.toString()
                    if(s.trim()==""){
                        Myutil.MyTost("必须输入仓位才能确定")
                    }else {
                        P.cjYuliaoCangwei(dialog.CangweiEt.text.toString())
                        dialog.dismiss()
                    }
                }
                dialog.RightBt.setOnClickListener {
                    dialog.dismiss()
                }
            }
            iv_header_left -> {
                finish()
            }
        }
    }

    override fun onItemClick(p0: AdapterView<*>?, p1: View?, position: Int, p3: Long) {
        val dialog = AlertDialog.Builder(this).create()
        dialog.setView(EditText(this))
        dialog.show()
        dialog.setContentView(R.layout.dialog_etyuliaocangwei)
        dialog.CangweiEt.setLines(20)
        val window = dialog.window
        dialog.title.text = "修改仓位"
        val p = window?.attributes // 获取对话框当前的参数值
        p?.width = (sp.getInt("width", 0) * 0.9).toInt() // 宽度设置为屏幕的0.65
        window?.attributes = p
        dialog.LeftBt.setOnClickListener {
            var s=dialog.CangweiEt.text.toString()
            if(s.trim()==""){
                Myutil.MyTost("必须输入仓位才能确定")
            }else {
                P.xgYuliaoCangwei(cangweilist[position].yfs_id, dialog.CangweiEt.text.toString())
                dialog.dismiss()
            }
        }
        dialog.RightBt.setOnClickListener {
            dialog.dismiss()
        }
    }

    override fun cjYuliaoCangweiOk(data: String) {
        Myutil.MyTost("创建仓位成功")
        updialog?.dismiss()
        P.getCangweiData()
        updialog = Myutil.showupdatedialog(this, "获取数据", false)
    }

    override fun cjYuliaoCangweiNo(data: String) {
        updialog?.dismiss()
    }

    override fun xgYuliaoCangweiOk(data: String) {
        Myutil.MyTost("修改仓位成功")
        updialog?.dismiss()
        P.getCangweiData()
        updialog = Myutil.showupdatedialog(this, "获取数据", false)
    }

    override fun xgYuliaoCangweiNo(data: String) {
        updialog?.dismiss()
    }

    override fun getCangweiDataOk() {
        updialog?.dismiss()
        Log.d("onSuccess",cangweilist.toString())
        adapter.notifyDataSetChanged()
    }

    override fun getCangweiDataNo() {
        updialog?.dismiss()
        Myutil.MyTost("获取数据失败")
    }

}